package com.ftg.learn.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.mall.entity.PmsBrand;
import com.ftg.learn.mall.service.IPmsBrandService;
import com.ftg.learn.util.BaseController;
import com.ftg.learn.util.ResEntity;
import com.ftg.learn.util.ResPageEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author mallCommon
 * @since 2022-04-19
 */
@RestController
@RequestMapping("/mall/pmsBrand")
public class PmsBrandController extends BaseController<PmsBrand, IPmsBrandService> {

    @Autowired
    IPmsBrandService iPmsBrandService;

    /**
     * 获取全部品牌列表
     * @return
     */
    @GetMapping("/findAllList")
    public ResEntity findAllList(){
        return new ResEntity(0,"success",iPmsBrandService.list());
    }


    /**
     * 分页获取品牌列表+模糊查询
     * @param keyword
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/findListPage")
    public ResPageEntity findListPage(@RequestParam(value = "keyword", required = false) String keyword,
                                      @RequestParam int page, @RequestParam int limit){
        IPage<PmsBrand> ip = new Page<>(page,limit);
        if (!StringUtils.isBlank(keyword)){
            QueryWrapper<PmsBrand> query = new QueryWrapper<>();
            query.like("brand_name",keyword);
            iPmsBrandService.page(ip,query);
            return new ResPageEntity(ip);
        }
        iPmsBrandService.page(ip);
        return new ResPageEntity(ip);
    }


    /**
     * 根据编号查询品牌信息
     */
    @GetMapping("/findOne/{id}")
    public ResEntity findOne(@PathVariable("id") Long id){
        QueryWrapper<PmsBrand> query = new QueryWrapper<>();
        query.eq("brand_id",id);
        return new ResEntity(0,"success",iPmsBrandService.list(query));
    }

    /**
     * 添加品牌信息
     */
    @PostMapping("/insert")
    public ResEntity createbrand(@RequestBody PmsBrand pmsBrand) {
        return super.service.save(pmsBrand)?new ResEntity() : new ResEntity(-1,"failure");
    }

    /**
     * 修改品牌信息
     */
    @PutMapping ("/updatebrand")
    public ResEntity updatebrand(@RequestBody PmsBrand pmsBrand) {
        return super.service.updateById(pmsBrand)?new ResEntity() : new ResEntity(-1,"failure");
    }

    /**
     * 删除品牌信息
     */
    @DeleteMapping("/delete")
    public ResEntity delbrand(@RequestParam Long id) {
        return super.service.removeById(id)?new ResEntity() : new ResEntity(-1,"failure");
    }

    /**
     * 修改显示状态
     * @param id
     * @param showStatus
     * @return
     */
    @PostMapping("/updateBrandStatus")

    public ResEntity updateShowStatus(@RequestParam List<Long> ids , @RequestParam Long showStatus) {
        int count = iPmsBrandService.updateBrandStatus(ids, showStatus);
        if (count > 0) {
            return new ResEntity(0, "成功修改" + count + "条");
        } else {
            return new ResEntity(-1, "修改失败");
        }
    }}
